﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace FusionDotNet
{
    /// <summary>
    /// Deletes a Google Fusion table.
    /// </summary>
    public class DeleteTableCommand : PostCommand<CommandResult>
    {
        /// <summary>
        /// Constructs a DeleteTableCommand.
        /// </summary>
        /// <param name="tableId">The ID of the table to delete.</param>
        public DeleteTableCommand(int tableId)
        {
            BuildDeleteString(tableId);
        }

        private void BuildDeleteString(int tableId)
        {
            if (tableId <= 0)
            {
                string message = "tableId must be greater than zero.";
                throw ExceptionBuilder.BuildException<ArgumentException>(message);
            }

            StringBuilder builder = new StringBuilder();
            builder.Append("sql=");
            builder.AppendFormat("DROP TABLE {0}", tableId);

            Encoding encoding = GetContentEncoding();
            PostContent = encoding.GetBytes(builder.ToString());
        }

        /// <summary>
        /// Creates a CommandResult based on the results of the command.
        /// </summary>
        /// <param name="reader">StreamReader containing the results of the operation.</param>
        /// <returns><see cref="CommandResult"/></returns>
        internal override CommandResult CreateResult(System.IO.StreamReader reader)
        {
            string status = reader.ReadLine();
            var result = new CommandResult();
            if (status == Constants.STATUS_OK)
            {
                result.Success = true;
            }
            else
            {
                result.Success = false;
            }
            return result;
        }
    }
}
